package com.eliza.music.view.music.login

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.rounded.ArrowForward
import androidx.compose.material.icons.rounded.AccountCircle
import androidx.compose.material.icons.rounded.Lock
import androidx.compose.material3.Checkbox
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme.colorScheme
import androidx.compose.material3.MaterialTheme.shapes
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextIndent
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil.compose.rememberAsyncImagePainter
import com.eliza.library.utils.compose.ImageBuild
import com.eliza.music.model.tmp.ResAvatar
import com.eliza.music.ui.theme.FontSize
import com.eliza.music.ui.theme.PaddingSize

@Preview
@Composable
private fun asd() {
    LoginX.Show()
}

object LoginX {
    @Composable
    fun Show() {
        Column(
            modifier = Modifier
                .fillMaxHeight()
                .background(Color.White)
                .padding(30.dp),
            verticalArrangement = Arrangement.SpaceEvenly,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            LoginTop()
            OperateScope()
        }
    }

    @Composable
    fun LoginTop(modifier: Modifier = Modifier) {
        Image(
            modifier = Modifier
                .size(150.dp)
                .clip(CircleShape),
            painter = rememberAsyncImagePainter(
                model = ImageBuild.CoilImageRequest(
                    LocalContext.current, ResAvatar.random()
                )
            ),
            contentDescription = "",
            contentScale = ContentScale.Crop
        )
    }

    @Composable
    fun OperateScope(modifier: Modifier = Modifier) {
        Column(
            modifier.fillMaxHeight(),
            verticalArrangement = Arrangement.SpaceEvenly,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {

            OperateData(modifier)
            HorizontalDivider(
                modifier = Modifier
                    .height(1.dp)
                    .background(Color(254, 254, 254))
            )
            var checked by remember { mutableStateOf(true) }
            Row(verticalAlignment = Alignment.CenterVertically) {
                Checkbox(checked = checked, onCheckedChange = { it ->
                    checked = it
                })
                Text(text = buildAnnotatedString {

                    append("已阅读并遵循")
                    withStyle(style = SpanStyle(color = Color.Blue)) {
                        append("《1984》")
                        append("《美丽新世界》")
                    }
                })
            }
            BottomIcons()
        }
    }

    @Composable
    fun OperateData(modifier: Modifier) {
        var username by remember {
            mutableStateOf("user")
        }
        OutlinedTextField(
            value = username,
            singleLine = true,
            shape = shapes.large,
            modifier = Modifier.fillMaxWidth(),
            colors = TextFieldDefaults.colors(
                focusedContainerColor = colorScheme.surface,
                unfocusedContainerColor = colorScheme.surface,
                disabledContainerColor = colorScheme.surface,
            ),
            onValueChange = { it ->
                username = it
            },
            label = {
                Icon(
                    imageVector = Icons.Rounded.AccountCircle,
                    contentDescription = "",
                    tint = Color.Gray
                )
            },
            textStyle = TextStyle(
                color = Color.Gray,
                fontSize = 16.sp,
                textAlign = TextAlign.Start,
                textIndent = TextIndent(2.sp)
            ),
        )
        OutlinedTextField(
            value = username,
            singleLine = true,
            shape = shapes.large,
            modifier = Modifier
                .fillMaxWidth(),
            colors = TextFieldDefaults.colors(
                focusedContainerColor = colorScheme.surface,
                unfocusedContainerColor = colorScheme.surface,
                disabledContainerColor = colorScheme.surface,
            ),
            onValueChange = { it ->
                username = it
            },
            label = {
                Icon(
                    imageVector = Icons.Rounded.Lock,
                    contentDescription = "",
                    tint = Color.Gray
                )
            },
            textStyle = TextStyle(
                color = Color.Gray,
                fontSize = FontSize.Normal.size,
                textAlign = TextAlign.Start,
                textIndent = TextIndent(2.sp)
            ),
        )
        Icon(
            modifier = Modifier
                .fillMaxSize(0.23f)
                .aspectRatio(1f)
                .border(2.dp, Color.Gray, CircleShape)
                .padding(PaddingSize.Normal.size),
            imageVector = Icons.AutoMirrored.Rounded.ArrowForward,
            contentDescription = "",
            tint = Color.Gray
        )
    }

    @Composable
    fun BottomIcons(modifier: Modifier = Modifier) {
        val imgBorder: @Composable (
            painter: Painter,
            clickFun: () -> Unit
        ) -> Unit = { painter, clickFun ->
            Image(
                modifier = Modifier
                    .size(100.dp)
                    .border(10.dp, Color.White.copy(0.2f), CircleShape)
                    .clip(CircleShape)
                    .clickable {
                        clickFun()
                    },
                painter = painter,
                contentDescription = "",
                contentScale = ContentScale.Crop
            )
        }
        Box(contentAlignment = Alignment.Center) {
            HorizontalDivider(
                modifier = Modifier
                    .height(1.dp)
                    .background(Color(254, 254, 254))
            )
            Text(
                buildAnnotatedString {
                    withStyle(style = SpanStyle(fontWeight = FontWeight.Bold)) {
                        append("Model: ")
                    }
                    append("llllllllllllllll")
                },
                fontSize = 16.sp,
                fontWeight = FontWeight.W500,
                modifier = Modifier.wrapContentSize(Alignment.TopCenter),
            )
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .wrapContentHeight(Alignment.Bottom),
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.SpaceEvenly
            ) {
                imgBorder(
                    rememberAsyncImagePainter(
                        model = ImageBuild.CoilImageRequest(
                            LocalContext.current, ResAvatar.random()
                        )
                    )
                ) {

                }
                imgBorder(
                    rememberAsyncImagePainter(
                        model = ImageBuild.CoilImageRequest(
                            LocalContext.current, ResAvatar.random()
                        )
                    )
                ) {

                }
                imgBorder(
                    rememberAsyncImagePainter(
                        model = ImageBuild.CoilImageRequest(
                            LocalContext.current, ResAvatar.random()
                        )
                    )
                ) {

                }
            }
        }
    }
}
